//
//  UIButton+FRP.m
//  Pods
//
//  Created by kepuna on 2017/10/18.
//
//

#import "UIButton+FRP.h"

@implementation UIButton (FRP)


+ (instancetype)nx_initWith:(void (^)(UIButton *btn))initBlock {
    UIButton *btn = [[UIButton alloc]init];
    if (initBlock) {
        initBlock(btn);
    }
    return btn;
}

- (UIButton *(^)(CGRect))nx_frame {
    return ^UIButton *(CGRect rect) {
        self.frame = rect;
        return self;
    };
}

- (UIButton *(^)(UIView *))nx_addInView {
    return ^UIButton *(UIView *view) {
        [view addSubview:self];
        return self;
    };
}

- (UIButton *(^)(NSString *, UIControlState))nx_imageForState {
    return ^UIButton *(NSString *name, UIControlState state) {
        [self setImage:[UIImage imageNamed:name] forState:state];
        return self;
    };
}

- (UIButton *(^)(NSString *, UIControlState))nx_titleForState {
    return ^UIButton *(NSString *title, UIControlState state) {
        [self setTitle:title forState:state];
        return self;
    };
}

- (UIButton *(^)(UIColor *, UIControlState))nx_titleColorForState {
    return ^UIButton *(UIColor *color, UIControlState state) {
        [self setTitleColor:color forState:state];
        return self;
    };
}

- (UIButton *(^)(UIColor *))nx_backgroundColor {
    return ^UIButton *(UIColor *color) {
        self.backgroundColor = color;
        return self;
    };
}

- (UIButton *(^)(UIColor *, UIControlState, BOOL))nx_backgroundColorForState {
    return ^UIButton *(UIColor *color, UIControlState state, BOOL isState) {
        switch (state) {
            case UIControlStateSelected:
                self.selected = isState;
                break;
                
            case UIControlStateHighlighted:
                self.highlighted = isState;
                break;
                
            case UIControlStateDisabled:
                self.enabled = !isState;
                break;
                
            default:
                break;
        }
        self.backgroundColor = color;
        return self;
    };
}

- (UIButton *(^)(CGFloat fontSize))nx_fontSize {
    return ^UIButton *(CGFloat fontSize) {
        self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        return self;
    };
}
- (UIButton *(^)(NSString *fontName ,CGFloat fontSize))nx_fontName {
    return ^UIButton *(NSString *fontName , CGFloat fontSize) {
        self.titleLabel.font = [UIFont fontWithName:fontName size:fontSize];
        return self;
    };
}

- (UIButton *(^)(UIControlContentHorizontalAlignment))nx_contentAlignment {
    return ^UIButton *(UIControlContentHorizontalAlignment alignment) {
        self.contentHorizontalAlignment = alignment;
        return self;
    };
}

- (UIButton *(^)(CGFloat))nx_cornerRadius {
    return ^UIButton *(CGFloat value) {
        self.layer.cornerRadius = value;
        self.layer.masksToBounds = YES;
        return self;
    };
}

- (UIButton *(^)(id target,SEL action))nx_targetAction {
    return ^UIButton *(id target,SEL selector) {
        [self addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}

- (UIButton *(^)(UIColor *, CGFloat))nx_borderAndWidth {
    return ^UIButton *(UIColor *color,CGFloat width) {
        self.layer.borderColor = color.CGColor;
        [self.layer setBorderWidth:width];
        return self;
    };
}

- (UIButton *(^)(BOOL hidden))nx_hidden {
    return ^UIButton *(BOOL hidden) {
        self.hidden = hidden;
        return self;
    };
    
}

- (UIButton *(^)(ButtonImagePositionStyle, CGFloat))nx_imagePositionAndSpace {
    return ^UIButton *(ButtonImagePositionStyle style,CGFloat spacing) {
        
        CGFloat imageW = self.imageView.image.size.width;
        CGFloat imageH = self.imageView.frame.size.height;
        CGFloat titleW = self.titleLabel.frame.size.width;
        CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
        CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
        
        if (style == ButtonImagePositionStyleDefault) {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
        } else if (style == ButtonImagePositionStyleRight) {
            
            CGFloat imageOffset = titleW + 0.5 * spacing;
            CGFloat titleOffset = imageW + 0.5 * spacing;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset, 0, - imageOffset);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
        } else if (style == ButtonImagePositionStyleTop) {
            
            self.imageEdgeInsets = UIEdgeInsetsMake(- titleIntrinsicContentSizeH - spacing, 0, 0, - titleIntrinsicContentSizeW);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
        } else if (style == ButtonImagePositionStyleBottom) {
            
            self.imageEdgeInsets = UIEdgeInsetsMake(titleIntrinsicContentSizeH + spacing, 0, 0, - titleIntrinsicContentSizeW);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, imageH + spacing, 0);
        }

//        CGFloat imageW = self.imageView.image.size.width;
//        CGFloat imageH = self.imageView.frame.size.height;
//        
//        CGFloat titleW = self.titleLabel.frame.size.width;
//        CGFloat titleH = self.titleLabel.frame.size.height;
//       
//        if (style == ButtonImagePositionStyleDefault) {
//            self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
//            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
//        } else if (style == ButtonImagePositionStyleRight) {
//            
//            CGFloat imageOffset = titleW + 0.5 * spacing;
//            CGFloat titleOffset = imageW + 0.5 * spacing;
//            self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset, 0, - imageOffset);
//            self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
//        } else if (style == ButtonImagePositionStyleTop) {
//            
//            self.imageEdgeInsets = UIEdgeInsetsMake(- titleH - spacing, 0, 0, - titleW);
//            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
//        } else if (style == ButtonImagePositionStyleBottom) {
//            
//            self.imageEdgeInsets = UIEdgeInsetsMake(titleH + spacing, 0, 0, - titleW);
//            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, imageH + spacing, 0);
//        }
        return self;
    };
}

- (UIButton *(^)(NSInteger, ButtonVertificationCode))nx_vertificationCodeForStyle {
    return ^UIButton *(NSInteger sec, ButtonVertificationCode style) {
        __block NSInteger tempSecond = sec;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(timer, ^{
            if (tempSecond <= 1) {
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.enabled = YES;
                    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
                });
            } else {
                tempSecond--;
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.enabled = NO;
                    NSString *second = (style == ButtonVertificationCodeSecond) ? @"s" : @"秒";
                    [self setTitle:[NSString stringWithFormat:@"%ld%@", (long)tempSecond,second] forState:UIControlStateNormal];
                });
            }
        });
        dispatch_resume(timer);
        return self;
    };
}

- (UIButton *(^)(NSInteger, ButtonVertificationCode, void (^)()))nx_vertificationCodeCompletionBlock {
    return ^UIButton *(NSInteger sec, ButtonVertificationCode style,void (^block)()) {
        
        __block NSInteger tempSecond = sec;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(timer, ^{
            if (tempSecond <= 1) {
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.enabled = YES;
                    block();
                });
            } else {
                tempSecond--;
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.enabled = NO;
                    NSString *second = (style == ButtonVertificationCodeSecond) ? @"s" : @"秒";
                    [self setTitle:[NSString stringWithFormat:@"%ld%@", (long)tempSecond,second] forState:UIControlStateNormal];
                });
            }
        });
        dispatch_resume(timer);
        return self;
    };
}

@end
